<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class youtube extends Admin_Controller {

    var $theme_data = array(
        'layout_main' => 'layout',
        'left' => 'include/leftcp',
    );
	var $model = "myoutube";
	var $view = "youtube";
    function __construct() {
        parent::__construct();
        $this->load->model($this->model);
		$this->resCode = $this->{$this->model}->resCode;
        $this->url_index = admin_url() . $this->view;
        $this->folder_upload = "dataupload/".$this->view."/";
        $this->url_edit = $this->url_index . "/edit/";
        $this->url_delete = $this->url_index . "/delete/";
        $this->url_add = $this->url_index . "/edit/0";
    }

    public function index() {
		redirect($this->url_index.'/adduser');
		/*
        $data = array();
        $data['datas'] = $this->{$this->model}->gets();
        $data['tresource'] = $this->mresource->get($this->resCode,'RES_CODE');
        $this->theme_data["main"] = $this->view."/index";
        $this->template->viewAdmin($this->theme_data, $data);
		*/
    }
	
    function edit($editID = 0) {
        $data = array();
		
        $post = $this->input->post();
        $data['edit'] = $this->{$this->model}->get($editID, $this->{$this->model}->primaryKey);
		$data['tresource'] = $this->mresource->get($this->resCode,'RES_CODE');
        $data['error'] = array('err_code' => 0, 'msg' => '');
        if ($post) {
			if($upload = $this->upload(@$data['edit']->OS_THUMB)){
				$post['edit']['OS_THUMB'] = $this->folder_upload.$upload['upload_data']['file_name'];
			}
            if ($editID > 0) {
                $this->{$this->model}->update($editID, $this->{$this->model}->primaryKey, $post['edit']);
            }
			else{
				$editID = $this->{$this->model}->insert($post['edit']);
			}
            redirect($this->url_edit . $editID);
        }
		$data['editID'] = $editID;
        $this->theme_data["main"] = $this->view."/edit";
        $this->template->viewAdmin($this->theme_data, $data);
    }
	
	function delete($deleteID = 0){
		if (!$this->permission->check("delete", $this->resCode))
			return $this->nopermission();
		if($deleteID > 0){
			$check = $this->{$this->model}->get($deleteID,'YOUT_ID');
			if($check->YOUT_TYPE == "user"){
				$this->deleteUser($deleteID);
			}
			else if($check->YOUT_TYPE == "playlist"){
				$this->deletePlayList($deleteID);
			}
			else {
				$this->{$this->model}->delete($deleteID, $this->{$this->model}->primaryKey);
			}
		}
	}

	function deleteUser($userID = 0){
		$playList = $this->{$this->model}->gets($userID,'YOUT_PARENT');
		foreach($playList as $r){
			$this->deletePlayList($r->YOUT_ID);
		}
		$this->{$this->model}->delete($userID, $this->{$this->model}->primaryKey);
	}
	
	function deletePlayList($playListID = 0){
		$this->{$this->model}->delete($playListID, 'YOUT_PARENT');
		$this->{$this->model}->delete($playListID, $this->{$this->model}->primaryKey);
	}
	
	function upload($old_name = '') {
		return ;
		$config['upload_path'] = './'.$this->folder_upload;
		$config['allowed_types'] = 'gif|jpg|png';
		
		if(!is_dir($this->folder_upload))
			mkdir($this->folder_upload,0777);
		$this->load->library('upload', $config);
		if ( ! $this->upload->do_upload())
		{
			return false;
		}
		else
		{
			if($old_name && isset($old_name))
			unlink('./'.$old_name);
			return array('upload_data' => $this->upload->data());
		}
		
	}
	
	function adduser($editID = 0){
		if (!$this->permission->check("select", $this->resCode))
			return $this->nopermission();
		$data = array();
        $data['datas'] = $this->{$this->model}->getsUser();
        $data['tresource'] = $this->mresource->get($this->resCode,'RES_CODE');
        $data['editID'] = $editID;
		$post = $this->input->post();
		$this->url_edit = $this->url_index . "/adduser/";
		if($editID>0)
			$data['edit'] = $this->{$this->model}->get($editID, $this->{$this->model}->primaryKey);
		if($post){
			$post['edit']['YOUT_TYPE'] = 'user';
			if ($editID > 0) {
                $this->{$this->model}->update($editID, $this->{$this->model}->primaryKey, $post['edit']);
            }
			else{
				$editID = $this->{$this->model}->insert($post['edit']);
				$insertPlayListDefault = array();
				$insertPlayListDefault['YOUT_PARENT'] = $editID;
				$insertPlayListDefault['YOUT_TYPE'] = 'playlist';
				$insertPlayListDefault['YOUT_LINK'] = '{playlistdefault}';
				$insertPlayListDefault['YOUT_TITLE'] = $post['edit']['YOUT_TITLE'];
				$insertPlayListDefault['YOUT_STATUS'] = 1;
				$this->{$this->model}->insert($insertPlayListDefault);
			}
            redirect($this->url_index.'/adduser');
		
		}
        $this->theme_data["main"] = $this->view."/adduser";
        $this->template->viewAdmin($this->theme_data, $data);
	
	}
	
	function addplaylist($editID = 0, $playListID = 0){
		if (!$this->permission->check("select", $this->resCode))
			return $this->nopermission();
		$data = array();
		$playlist = array();
        $data['datas'] = $this->{$this->model}->getsPlayList($editID,'YOUT_PARENT');
        $data['info'] = $this->{$this->model}->get($editID,'YOUT_ID');
        $data['tresource'] = $this->mresource->get($this->resCode,'RES_CODE');
        $data['editID'] = $editID;
        $data['playListID'] = $playListID;
		$post = $this->input->post();
		$this->url_edit = $this->url_index . "/addplaylist/".$editID."/";
		if($playListID>0)
			$data['edit'] = $this->{$this->model}->get($playListID, $this->{$this->model}->primaryKey);
		if($post){
			if($this->input->post("getAddNew") !=""){
				 $edit = $this->input->post("edit");
				$post['edit']['YOUT_TYPE'] = 'playlist';
				$post['edit']['YOUT_PARENT'] =$editID;
				if ($playListID > 0) {
					$this->{$this->model}->update($playListID, $this->{$this->model}->primaryKey, $post['edit']);
				}
				else{
					$playListID = $this->{$this->model}->insert($post['edit']);
				}
			}
			else {
			if($post && $data['info']->YOUT_LINK != ''){
				$this->myoutube->getPlaylistByUser($playlist,$data['info']->YOUT_LINK, $data['info']->YOUT_ID);
				$arr_insert = array();
				for($i=count($playlist) -1; $i>=0; $i--){
					$arr_insert[] = $playlist[$i];
				}
				$result = $this->myoutube->insertPlayList($arr_insert);
				}
			}
		redirect($this->url_index.'/addplaylist/'.$editID);
		}
        $this->theme_data["main"] = $this->view."/addplaylist";
        $this->template->viewAdmin($this->theme_data, $data);
	}
	
	function addvideo($editID = 0){
		if (!$this->permission->check("select", $this->resCode))
			return $this->nopermission();
		$data = array();
		$videos = array();
        $data['datas'] = $this->{$this->model}->getsVideo($editID,'YOUT_PARENT');
        $data['info'] = $this->{$this->model}->get($editID,'YOUT_ID');
        $data['tresource'] = $this->mresource->get($this->resCode,'RES_CODE');
        $data['editID'] = $editID;
		$post = $this->input->post();
		$this->url_edit = $this->url_index . "/addvideo";
		if($editID>0)
			$data['edit'] = $this->{$this->model}->get($editID, $this->{$this->model}->primaryKey);
		if($post && $data['edit']->YOUT_LINK != ''){
			if(trim($data['edit']->YOUT_LINK) =='{playlistdefault}'){
				$youtubeUser = $this->{$this->model}->get($data['edit']->YOUT_PARENT, $this->{$this->model}->primaryKey);
				$this->myoutube->getVideoByUser($videos,$youtubeUser->YOUT_LINK, $data['edit']->YOUT_ID);
			}
			else {
				$this->myoutube->getVideoByPlayList($videos,$data['edit']->YOUT_LINK, $data['edit']->YOUT_ID);
			}
			$arr_insert = array();
			for($i=count($videos) -1; $i>=0; $i--){
				$arr_insert[] = $videos[$i];
			}
		 
			 $result = $this->myoutube->insertVideos($arr_insert);
			redirect($this->url_index.'/addvideo/'.$editID);
		
		}
        $this->theme_data["main"] = $this->view."/addvideo";
        $this->template->viewAdmin($this->theme_data, $data);
	}
	
	function transuser($userID = 0){
		$this->load->model("mvideo");
        $this->load->model("mprogram");
        $this->load->model("mseries");
		
		$user = $this->{$this->model}->getUser($userID,'YOUT_ID');
		if($user && $user->YOUT_STATUS==1){
			$insert_program = array(
				'PROG_TITLE'=>$user->YOUT_TITLE,
				'CAT_ID'=>$user->CAT_ID,
				'PROG_CODE'=> $this->seo->seolink($user->YOUT_TITLE),
				'PROG_STATUS'=>1,
			);
			$pro_id = $this->mprogram->insert($insert_program);
			$this->myoutube->update($user->YOUT_ID,'YOUT_ID',array('MY_ID'=>$pro_id,'YOUT_STATUS'=>2));
			$this->myoutube->reload_cache();
		}
			
		redirect($this->url_index.'/adduser');
	}
	function transplaylist($playListID = 0){
		$this->load->model("mvideo");
        $this->load->model("mprogram");
        $this->load->model("mseries");
		
		$playlist = $this->{$this->model}->getPlayList($playListID);
		if($playlist && $playlist->YOUT_STATUS==1){
			$p = $this->{$this->model}->get($playlist->YOUT_PARENT,'YOUT_ID');
			if($p->MY_ID <=0)
				die("User not add !!");
			$insert_series = array(
				'SER_TITLE'=>$playlist->YOUT_TITLE,
				'PROG_ID'=>$p->MY_ID,
				'SER_CODE'=> $this->seo->seolink($playlist->YOUT_TITLE),
				'SER_STATUS'=>1,
			);
			$series_id = $this->mseries->insert($insert_series);
			$this->myoutube->update($playlist->YOUT_ID,'YOUT_ID',array('MY_ID'=>$series_id,'YOUT_STATUS'=>2));
		}
			
		redirect($this->url_index.'/addplaylist/'.$playlist->YOUT_PARENT);
	}
	function transvideo($videoID = 0){
		$this->load->model("mvideo");
        $this->load->model("mprogram");
        $this->load->model("mseries");
		
		$video = $this->{$this->model}->getVideo($videoID);
		if($video && $video->YOUT_STATUS==1){
			$p = $this->{$this->model}->get($video->YOUT_PARENT,'YOUT_ID');
			if($p->MY_ID <=0)
				die("Playlist not add !!");
			$video_time = $this->myoutube->pareTimeYoutube($video->YOUT_LINK);
			if($video_time <=0)
				die("Video deleted");
			$check = $this->mvideo->get($video->YOUT_LINK,'VID_LINK');
			if(!$check){
			$insert_video = array(
				'VID_TITLE'=>$video->YOUT_TITLE,
				'SER_ID'=>$p->MY_ID,
				'VID_CODE'=> $this->seo->seolink($video->YOUT_TITLE),
				'VID_VIEW'=>1,
				'VID_TIME'=>$video_time,
				'VID_LINK'=>$video->YOUT_LINK,
				'VID_TYPE'=>'youtube.com',
			);
			$video_id = $this->mvideo->insert($insert_video);
			}
			else {
				$video_id = $check->VID_ID;
			}
			$this->myoutube->update($video->YOUT_ID,'YOUT_ID',array('MY_ID'=>$video_id,'YOUT_STATUS'=>2));
		}
			
		redirect($this->url_index.'/addvideo/'.$video->YOUT_PARENT);
	}
}
